package cn.edu.neusoft.yuejian4.yuejian_4.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import cn.edu.neusoft.yuejian4.yuejian_4.R;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.Success;
import cn.edu.neusoft.yuejian4.yuejian_4.listener.TListener;
import cn.edu.neusoft.yuejian4.yuejian_4.model.LoginModel;
import cn.edu.neusoft.yuejian4.yuejian_4.model.MyModel;

/**
 * Created by Lix on 2018/10/login_bg2.
 */

public class MySetPasswordActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView back;
    private EditText newPwd,againPwd;
    private Button saveSet;
    private SharedPreferences sp;
    private int uid;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_setpasswordt);
        sp = getSharedPreferences("userinfo", MODE_PRIVATE);
        uid = sp.getInt("uid", 4);
        initView();
        initEvent();
    }
    private void initView(){
        back = (ImageView)findViewById(R.id.my_set_pwd_back);
        newPwd = (EditText)findViewById(R.id.my_set_newpwd);
        againPwd = (EditText)findViewById(R.id.my_set_againpwd);
        saveSet = (Button)findViewById(R.id.btn_save_update_pwd);


    }
    private void initEvent(){
        back.setOnClickListener(this);
        saveSet.setOnClickListener(this);
    }
    TListener<Success> updatepassword = new TListener<Success>() {
        @Override
        public void onResponse(Success success) {
            Toast.makeText(MySetPasswordActivity.this,"修改密码成功", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(MySetPasswordActivity.this,LoginActivity.class);
            startActivity(intent);
            finish();
        }

        @Override
        public void onFail(String msg) {

        }
    };
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.my_set_pwd_back:
                finish();
                break;
            case R.id.btn_save_update_pwd:
                String newpwd = newPwd.getText().toString();
                String againpwd = againPwd.getText().toString();
                if(newpwd==""||newpwd==null||againpwd==""||againpwd==null){
                    Toast.makeText(MySetPasswordActivity.this,"输入不能为空", Toast.LENGTH_SHORT).show();
                }else if(!newpwd.equals(againpwd)){
                    againPwd.setError("两次输入不一致");
                }else {
                    MyModel myModel = new MyModel();
                    myModel.updatePassword(uid,newpwd,updatepassword);
                }

                break;
        }
    }
}
